<?php
/**
 * @ClassName: Amap
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-16 18:46
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\logic\AmapLogic;
use think\Controller;

class Amap extends Controller
{
    /**
     * 地理编码
     * @return \think\response\Json
     */
    public function geo()
    {
        $address = input('address', '');

        return json(AmapLogic::geo($address));
    }

    /**
     * 逆地理编码
     * @return \think\response\Json
     */
    public function regeo()
    {
        $longitude = input('longitude', ''); //经度
        $latitude = input('latitude', ''); //纬度
        $config = config('map.amap');
//        var_dump($longitude, $latitude);
//        var_dump($config);

        $data = [
            'province' => '定位中...',
            'city' => '定位中...',
            'area' => '定位中...',
            'town' => '定位中...',
            'street' => '定位中...',
            'address' => '',
            'areaCode' => '53',
        ];

        if ($longitude && $latitude) {
            $res = file_get_contents("https://restapi.amap.com/v3/geocode/regeo?output=json&location={$longitude},{$latitude}&key={$config['key']}");
            $res = json_decode($res, true);
            if (isset($res['regeocode']['addressComponent'])) {
                $data['province'] = $res['regeocode']['addressComponent']['province'];
                $data['city'] = $res['regeocode']['addressComponent']['city'];
                $data['area'] = $res['regeocode']['addressComponent']['district'];
                $data['town'] = $res['regeocode']['addressComponent']['township'];
                $data['areaCode'] = $res['regeocode']['addressComponent']['towncode'];
                if (isset($res['regeocode']['addressComponent']['streetNumber'])) {
                    $data['street'] = $res['regeocode']['addressComponent']['streetNumber']['street'];
                }
            }
            if (isset($res['regeocode']['formatted_address'])) {
                $data['address'] = $res['regeocode']['formatted_address'];
            }
        }

        return json(['code' => 1, 'msg' => 'OK', 'data' => $data]);
    }

    /**
     * 两点间距离测量
     * @return \think\response\Json
     */
    public function distance()
    {
        $origins = input('origins', ''); //出发点，支持100个坐标对，坐标对之间用'|'分隔
        $destination = input('destination', ''); //目的地，规则： lon,lat（经度,纬度）,支持一个
        //路径计算的方式和方法
        //0：直线距离
        //1：驾车导航距离（仅支持国内坐标）。其中必须特别指出，当为1 的时候会考虑路况因素，故返回结果可能存在不同
        //2：公交规划距离（仅支持同城坐标）
        //3：步行规划距离（仅支持5km之间的距离）
        $type = input('$type', 1);
        $distance = '未知';
        if ($origins && $destination) {
            $res = amap()->distance($origins, $destination, $type);
            $distance = $res ? bcdiv($res['results'][0]['distance'], 1000, 4) : 0;
        }

        return json(['code' => 1, 'msg' => 'OK', 'data' => $distance]);
    }

    /**
     * IP定位
     * @return \think\response\Json
     */
    public function getIpRegion()
    {
        $ip = input('ip', '');
        if ($ip) {
            try {
                $res = amap()->ip($ip);
                return json(['code' => 1, 'msg' => 'OK', 'data' => $res]);
            } catch (\Exception $e) {
                $this->error($e);
            }
        } else {
            $this->error('IP地址为空！');
        }
    }

    /**
     * 天气查询
     * @return \think\response\Json
     */
    public function weather()
    {
        $cityCode = input('cityCode', '530102'); //城市名称，填写adcode
        $type = input('type', 'base'); //气象类型，base:返回实况天气，all:返回预报天气
        $res = amap()->weather($cityCode, $type);

        return json(['code' => 1, 'msg' => 'OK', 'data' => $res]);
    }

}